﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CinemaBookingUI.WCFReference;

namespace CinemaBookingUI
{
    public partial class CustomerSearch : Form
    {
        private WCFReference.IWcfCinemaService wcf = SingletonWCF.Instance.WCFNetTcp();
        private static bool isOpen = false;

        public CustomerSearch(string phoneNo)
        {
            InitializeComponent();
            dgvCustomers.DataSource = Customers(phoneNo);
            dgvCustomers.VirtualMode = true;
            
        }

        private void dgvCustomers_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string phone = "";
                string name = "";
                string email = "";
                Booking booked = null;
                foreach (DataGridViewRow row in dgvCustomers.Rows)
                {
                    if (row.Index == e.RowIndex)
                    {
                        phone = row.Cells["CustomerPhoneNo"].Value.ToString();
                        name = row.Cells["CustomerName"].Value.ToString();
                        email = row.Cells["CustomerEmail"].Value.ToString();
                        booked = wcf.ReadBookingById(Convert.ToInt32(row.Cells["CustomerBooking"].Value.ToString()));
                    }
                }

                TicketInfo ticForm = new TicketInfo(booked);
                ticForm.Show();

            }
            catch (Exception)
            {

                MessageBox.Show("Der er ingen reservationer tilknyttet den givne kunde");
            }
        }

        private void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (!isOpen)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    RefreshSearch();
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            RefreshSearch();
        }

        private void RefreshSearch()
        {
            string txt = txtSearch.Text.Trim();
            int phone;
            bool isPhone = int.TryParse(txt, out phone);
            if (isPhone)
            {
                dgvCustomers.DataSource = Customers(txtSearch.Text);
            }
            else
            {
                string mes = "Du skal indtaste et telefon nummer i søgefeltet.\r\nTryk på OK knappen eller tryk på Escape tasten.";
                string cap = "Fejl";
                MessageBoxButtons but = MessageBoxButtons.OK;
                DialogResult result;
                result = MessageBox.Show(mes, cap, but);
                isOpen = true;

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    isOpen = false;
                }
            }
        }

        private List<Customer> Customers(string phone)
        {
            return wcf.ReadCustomer(phone);
        }

        private void dgvCustomers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //string propName = dgvCustomers.Columns[e.ColumnIndex].DataPropertyName;
            //object propVal = dgvCustomers.Rows[e.RowIndex].DataBoundItem;

            //if (propName.Contains('.') && propVal != null)
            //{
            //    while (propName.Contains('.') && propVal != null)
            //    {
            //        propVal = propVal.GetType().GetProperty(propName.Substring(0, propName.IndexOf('.'))).GetValue(propVal, null);
            //        propName = propName.Substring(propName.IndexOf('.') + 1);
            //    }
            //    if (propVal != null)
            //    {
            //        e.Value = propVal.GetType().GetProperty(propName).GetValue(propVal, null);
            //    }
            //}

        }
    }
}
